package fr.upmc.cie;

import fr.upmc.cie.data.CategorieItem;
import fr.upmc.cie.data.ChannelItem;
import fr.upmc.cie.data.DataManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class ChannelDisplay extends Activity{

	private ChannelDisplay self = this;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.channel_modif);
		
		ChannelItem idCateg = (ChannelItem) this.getIntent().getExtras().getSerializable("channel");

		final Button btModifchannel = (Button) this.findViewById(R.id.Button01);
		final Button btSuppChannel = (Button) this.findViewById(R.id.boutDelete);
		final EditText channel_title = (EditText) this.findViewById(R.id.fluxDispurl);
		final EditText channel_url = (EditText) this.findViewById(R.id.FluxDipurl);
		final ChannelItem itemchannel =DataManager.getChannelManager(self).getChannelById(idCateg.getId(), self);
		final Spinner categorie = (Spinner) this.findViewById(R.id.channel_modif_category);
		
		
		SpinnerAdapter sa = new ArrayAdapter<CategorieItem>(this, android.R.layout.simple_spinner_item, DataManager.getCategorieManager(this).getAllCategories());
		categorie.setAdapter(sa);
		categorie.setSelection((int) (DataManager.getChannelManager(self).getChannelCategorieId(itemchannel)-1));
		
		channel_title.setText(itemchannel.getTitle());
		channel_url.setText(itemchannel.getUrl());
		channel_url.setEnabled(false);
		btModifchannel.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
			itemchannel.setTitle(channel_title.getText().toString());
			itemchannel.setUrl(channel_url.getText().toString());
			
			DataManager.getChannelManager(self).updateChannel(itemchannel, self);
			DataManager.getChannelManager(self).setChannelCategorie(itemchannel, (CategorieItem) categorie.getSelectedItem());
			Intent i = new Intent();
			self.setResult(2,i);
			finish();
			}
		});
		
		btSuppChannel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				DataManager.getChannelManager(self).deleteChannel(itemchannel, self);

				finish();
			}
		});
		
	}
}


